//
//  LoginBacgroundPlayer.m
//  MELive
//
//  Created by 曹曹 on 2016/10/19.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "LoginBacgroundPlayer.h"

@interface LoginBacgroundPlayer ()

@end

@implementation LoginBacgroundPlayer

- (instancetype)initWithUrlForResource:(NSString *)resource type:(NSString *)type frame:(CGRect)frame {
    NSURL *url = [[NSBundle mainBundle] URLForResource:resource withExtension:type];
    AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
    self = [super initWithPlayerItem:playerItem];
    if (self) {
        _playerLayer = [AVPlayerLayer playerLayerWithPlayer:self];
        _playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
        _playerLayer.frame = frame;
        [self play];
        /** 监测视频播放完成 */
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    }
    return self;
}

/** 从头播放 */
- (void)moviePlayEnd:(NSNotificationCenter *)notification {
    if (_repeat) {
        CMTime time = CMTimeMake(0, 1);
        [self seekToTime:time];
        [self play];
    }
}

@end
